﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Exam.Core.DBUtility;
using System.Data;

namespace Exam.Core.DAL {
    public class QuestionType {

        private readonly string path = FilePath.QuestionTypeXmlPath.Trim();

        //返回所有
        public List<Exam.Core.Model.QuestionType> getQuestionTypeList() {
            
            XmlDocument doc = XmlHelper.GetXmlDocument(path);
            List<Exam.Core.Model.QuestionType> list = new List<Exam.Core.Model.QuestionType>();
            XmlNodeList nodeList = doc.GetElementsByTagName("questiontype");
            foreach (XmlNode node in nodeList) {
                Exam.Core.Model.QuestionType obj = new Exam.Core.Model.QuestionType();
                obj.QuestionTypeId = node.Attributes["id"].Value.Trim();
                obj.QuestionTypeTitle = node.Attributes["title"].Value.Trim();
                obj.QuestionTypeAttr = node.Attributes["questiontypeattr"].Value.Trim();
                obj.QuestionCount = new Exam.Core.DAL.Question().getCountByType(obj.QuestionTypeId);
                list.Add(obj);
            }
            return list;

        }

        internal List<Exam.Core.Model.QuestionType> getQuestionTypeListByCourseId(string courseid) {
            List<Exam.Core.Model.QuestionType> list = new List<Exam.Core.Model.QuestionType>();
            string SQL = "SELECT DISTINCT QuestionTypeId FROM Question WHERE CourseId="+courseid;
            DataTable dt = DbHelperOleDb.Query(SQL).Tables[0];
            XmlDocument doc = XmlHelper.GetXmlDocument(path);
            XmlNodeList nodeList = doc.GetElementsByTagName("questiontype");
            foreach (DataRow dr in dt.Rows) {
                foreach (XmlNode node in nodeList){
                    if (node.Attributes["id"].Value.Trim() == dr["QuestionTypeId"].ToString().Trim()) {
                        Exam.Core.Model.QuestionType obj = new Exam.Core.Model.QuestionType();
                        obj.QuestionTypeId = node.Attributes["id"].Value.Trim();
                        obj.QuestionTypeTitle = node.Attributes["title"].Value.Trim();
                        obj.QuestionTypeAttr = node.Attributes["questiontypeattr"].Value.Trim();
                        obj.QuestionCount = new Exam.Core.DAL.Question().getCountByType(obj.QuestionTypeId, courseid);
                        list.Add(obj);
                    }
                }
            }

            return list;
        }
    }
}
